package com.thl.filechooser;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: liujie
 * @date: 2022/6/17 上午9:22
 * @description:
 */
public final class FileString implements Comparable<FileString> {
    public String fileName;
    public long prefix_num;
    /**
     * 标识不是带数字的文件或者文件夹
     */
    public static final int PREFIX_NUM_FINAL = -2;
    public FileInfo fileInfo;
    Pattern number = Pattern.compile("(\\d+)\\..*");

    public FileString(String fileName, FileInfo fileInfo) {
        this.fileName = fileName;
        this.fileInfo = fileInfo;
        Matcher matcher = number.matcher(fileName);
        if (matcher.find()) {
            String num = matcher.group(1);
            if (TextUtils.isEmpty(num)){
                prefix_num = PREFIX_NUM_FINAL;
            }else {
                if (num.length()>=19){
                    num = num.substring(0,18);
                }
                prefix_num = Long.parseLong(num);
            }
        } else {
            prefix_num = PREFIX_NUM_FINAL;
        }
    }

    @Override
    public int compareTo(FileString o) {
        if (prefix_num != PREFIX_NUM_FINAL) {
            return o.prefix_num > prefix_num ? -1 : o.prefix_num == prefix_num ? 0 : 1;
        } else {
            return this.fileName.toLowerCase().compareTo(o.fileName.toLowerCase());
        }
    }

    @Override
    public String toString() {
        return fileName;
    }
}